﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Incubator.Controls
{
    public partial class Sell : System.Web.UI.UserControl
    {
        List<RadioButton> rb_list = new List<RadioButton>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null || (int)Session["UserID"] == -1)
                Response.Redirect("main.aspx");
            //get list of shares from market place

            webRef.User u = (webRef.User)Session["User"];
            webRef.Company c = Global.incuServ.GetCompany(u.company_id);

            webRef.Share[] shares = Global.incuServ.GetAllUserShares(u);
            //show them in the table

            const string group_name = "Market Place";

            rb_list.Clear();

            foreach (webRef.Share s in shares)
            {
                if (s.isOnMarket == false)
                {
                    TableCell tc0 = new TableCell();
                    TableCell tc1 = new TableCell();
                    TableCell tc2 = new TableCell();

                    TableRow tr = new TableRow();

                    RadioButton rb = new RadioButton();
                    rb.Text = s.company.name;
                    rb.GroupName = group_name;
                    rb_list.Add(rb);
                    tc0.Controls.Add(rb);

                    tc1.Controls.Add(new LiteralControl(s.owner.username));
                    tc2.Controls.Add(new LiteralControl(s.quantity.ToString()));


                    tr.Cells.Add(tc0);
                    tr.Cells.Add(tc1);
                    tr.Cells.Add(tc2);

                    tblShares.Rows.Add(tr);
                }
            }
        }

       
        protected void btnBuy_Click(object sender, EventArgs e)
        {
            Response.Redirect("Buy.aspx");
        }

        protected void btnPut_Click(object sender, EventArgs e)
        {
            //know which share is selected
            //get list of shares from market place

            webRef.User u = (webRef.User)Session["User"];
            webRef.Company c = Global.incuServ.GetCompany(u.company_id);

            webRef.Share[] shares = Global.incuServ.GetAllUserShares(u);
            //know which share is selected
            int i = 0;
            foreach (RadioButton rb in rb_list)
            {
                while (shares[i].isOnMarket)
                    i++;
                if (rb.Checked)
                {
                    try{
                        Global.incuServ.PutShareOnMarket(shares[i].owner, shares[i].company, Int32.Parse(tbQuant.Text), Int32.Parse(tbPrice.Text));
                        Response.Redirect("Sell.aspx");
                    }
                    catch (Exception ex) {
                        lbError.Text = "Error: Not Enough Shares to Sell!" ;
                    }
                    
                    return;
                }
                i++;
            }
            //no radio button has been selected.

        }

        protected void btnGetInfo_Click(object sender, EventArgs e)
        {
            //know which share is selected
            //get list of shares from market place

            webRef.User u = (webRef.User)Session["User"];
            webRef.Company c = Global.incuServ.GetCompany(u.company_id);

            webRef.Share[] shares = Global.incuServ.GetAllUserShares(u);
            //know which share is selected

            int i = 0;
            foreach (RadioButton rb in rb_list)
            {
                while (shares[i].isOnMarket)
                    i++;
                if (rb.Checked)
                {
                    //get the share's company
                    Session["CompanyOverview"] = shares[i].company;
                    //jump to the CompanyOverview page
                    Response.Redirect("CompanyOverview.aspx");
                    return;
                }
                i++;
            }
            //no radio button has been selected.
        }
    }
}